﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace TPCN.DAL
{
   public class KhachHangDAL
    {
        public static KhachHangObj GetByID(string KhachID)
        {
            KhachHangObj obj = new KhachHangObj();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "sp_tbKhachHang_GetByID";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@KhachID", KhachID);
                using (SqlDataReader dr = Database.db.GetDataToDataReader(cmd))
                {
                    if (dr.HasRows)
                    {
                        dr.Read();
                        obj = obj.SetValuesFromDataReader(dr);
                    }
                    dr.Close();
                }
            }
            return obj;
        }

        public static KhachHangObj GetByEmail(string Email)
        {
            KhachHangObj obj = new KhachHangObj();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "sp_tbKhachHang_GetByEmail";
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Email", Email);
                using (SqlDataReader dr = Database.db.GetDataToDataReader(cmd))
                {
                    if (dr.HasRows)
                    {
                        dr.Read();
                        obj = obj.SetValuesFromDataReader(dr);
                    }
                    dr.Close();
                }
            }
            return obj;
        }

        public static List<KhachHangObj> GetByAll()
        {
            List<KhachHangObj> result = new List<KhachHangObj>();
            KhachHangObj obj = new KhachHangObj();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "sp_tbKhachHang_GetByAll";
                cmd.CommandType = CommandType.StoredProcedure;
                using (SqlDataReader dr = Database.db.GetDataToDataReader(cmd))
                {
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            result.Add(obj.SetValuesFromDataReader(dr));
                        }
                    }
                    dr.Close();
                    obj = null;
                }
            }
            return result;
        }

        public static int Insert(KhachHangObj obj)
        {
            int id = 0;
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "sp_tbKhachHang_Insert";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Hovaten", (obj.Hovaten == null) ? String.Empty : obj.Hovaten);
                    cmd.Parameters.AddWithValue("@Gioitinh", (obj.Gioitinh == null) ? String.Empty : obj.Gioitinh);
                    cmd.Parameters.AddWithValue("@Sodienthoai", (obj.Sodienthoai == null) ? String.Empty : obj.Sodienthoai);
                    cmd.Parameters.AddWithValue("@Email", (obj.Email == null) ? String.Empty : obj.Email);
                    cmd.Parameters.AddWithValue("@Password", (obj.Password == null) ? String.Empty : obj.Password);
                    cmd.Parameters.AddWithValue("@Active", (obj.Active == null) ? String.Empty : obj.Active);
                    cmd.Parameters.AddWithValue("@Diachi", (obj.Diachi == null) ? String.Empty : obj.Diachi);

                    SqlParameter p = new SqlParameter("@ID", DbType.Int32);
                    p.Direction = ParameterDirection.Output;
                    cmd.Parameters.Add(p);
                    
                    Database.db.ExecuteNonQuery(cmd);

                    id = Int32.Parse(cmd.Parameters["@ID"].Value.ToString());
                }
                return id;
            }
            catch (Exception)
            {
                return 0;
            }
        }

        public static bool Update(KhachHangObj obj)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "sp_tbKhachHang_Update";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@KhachID", (obj.KhachID == null) ? String.Empty : obj.KhachID);
                    cmd.Parameters.AddWithValue("@Hovaten", (obj.Hovaten == null) ? String.Empty : obj.Hovaten);
                    cmd.Parameters.AddWithValue("@Gioitinh", (obj.Gioitinh == null) ? String.Empty : obj.Gioitinh);
                    cmd.Parameters.AddWithValue("@Sodienthoai", (obj.Sodienthoai == null) ? String.Empty : obj.Sodienthoai);
                    cmd.Parameters.AddWithValue("@Email", (obj.Email == null) ? String.Empty : obj.Email);
                    cmd.Parameters.AddWithValue("@Password", (obj.Password == null) ? String.Empty : obj.Password);
                    cmd.Parameters.AddWithValue("@Active", (obj.Active == null) ? String.Empty : obj.Active);
                    cmd.Parameters.AddWithValue("@Diachi", (obj.Diachi == null) ? String.Empty : obj.Diachi);
                   Database.db.ExecuteNonQuery(cmd);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public static bool Delete(string KhachID)
        {
            try
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "sp_tbKhachHang_DeleteByID";
                    cmd.Parameters.AddWithValue("@KhachID", KhachID);
                    Database.db.ExecuteNonQuery(cmd);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}
